package com.yasaka.stock.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yasaka.stock.common.*;
import com.yasaka.stock.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.xm.xmnlp.Xmnlp;
import org.xm.xmnlp.corpus.tag.Nature;
import org.xm.xmnlp.seg.domain.Term;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MlpService {

    public TWebResult getOnLineCountWord(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        List<TCountWord> mList = new ArrayList<TCountWord>();
        mList.add(CountWord.wordCount(aText, null));
        return new TWebResult(mList);
    }

    public TWebResult getOnlineJapaneseName(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        List<String> mList = TXMNLP.getJapaneseName(aText);
        TWebResult mResult = new TWebResult();
        mResult.setResult(true);
        mResult.setData(mList);
        return mResult;
    }

    public TWebResult getTranslatedName(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        TWebResult mResult = new TWebResult();
        mResult.setResult(true);
        mResult.setData(TXMNLP.getTermforNature(aText, Nature.nrf));
        return mResult;
    }

    public TWebResult getChineseName(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        TWebResult mResult = new TWebResult();
        mResult.setResult(true);
        mResult.setData(TXMNLP.getTermforNature(aText, Nature.nr));
        return mResult;
    }

    public TWebResult getPinyinForFileId(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        TWebResult mResult = new TWebResult();
        mResult.setResult(true);
        mResult.setData(Xmnlp.convertToPinyinList(aText));
        return mResult;
    }

    public TWebResult getOrganization(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        TWebResult mResult = new TWebResult();
        mResult.setResult(true);
        mResult.setData(TXMNLP.getTermforNature(aText, Nature.nt));
        return mResult;
    }

    public TWebResult googleTranslation(String aStr, String aFrom, String aTo) {
        List<String> mList = new ArrayList<String>();
        try {
            mList.add(GoogleTranslate.translate(aStr, LanguageList.getGoogleLanguageForTransnId(aFrom),
                    LanguageList.getGoogleLanguageForTransnId(aTo)));
        } catch (Exception e) {
            return new TWebResult(TApiState.API116, e.getMessage(), false);
        }
        return new TWebResult(mList);
    }

    public TWebResult getNumberAndQuantifier(String aText) {
        if (StringUtils.isBlank(aText)) {
            return new TWebResult(TApiState.API102, false);
        }
        Map<String, List<String>> mMap = new HashMap<String, List<String>>();
        List<String> mNumerals = new ArrayList<String>();
        List<String> mQuantifiers = new ArrayList<String>();
        List<String> mChineseEra = new ArrayList<String>();
        List<String> mQuantifier = new ArrayList<String>();
        List<String> mVerbalMeasureWord = new ArrayList<String>();
        List<String> mTimeMeasureWord = new ArrayList<String>();
        List<Map<String, List<String>>> mList = new ArrayList<Map<String, List<String>>>();
        mList.add(mMap);

        mMap.put("Numerals", mNumerals); //数词
        mMap.put("Quantifiers", mQuantifiers);//数量词
        mMap.put("ChineseEra", mChineseEra); //中国纪年词 天干地支等，如，甲已丙丁等
        mMap.put("Quantifier", mQuantifier);//量词
        mMap.put("VerbalMeasureWord", mVerbalMeasureWord); //动量词
        mMap.put("TimeMeasureWord", mTimeMeasureWord);//时量词
        for (Term mterm : TXMNLP.segment.seg(aText)) {
            if (mterm.nature == Nature.m) {
                if (!mNumerals.contains(mterm.word))
                    mNumerals.add(mterm.word);
            } else if (mterm.nature == Nature.mq) {
                if (!mQuantifiers.contains(mterm.word))
                    mQuantifiers.add(mterm.word);
            } else if (mterm.nature == Nature.Mg) {
                if (!mChineseEra.contains(mterm.word))
                    mChineseEra.add(mterm.word);
            } else if (mterm.nature == Nature.q) {
                if (!mQuantifier.contains(mterm.word))
                    mQuantifier.add(mterm.word);
            } else if (mterm.nature == Nature.qv) {
                if (!mVerbalMeasureWord.contains(mterm.word))
                    mVerbalMeasureWord.add(mterm.word);
            } else if (mterm.nature == Nature.qt) {
                if (!mTimeMeasureWord.contains(mterm.word))
                    mTimeMeasureWord.add(mterm.word);
            }
        }
        return new TWebResult(mList);
    }

    public TWebResult getStrlang(String aStr) {
        if (StringUtils.isBlank(aStr)) {
            return new TWebResult(TApiState.API102, false);
        }
        String mStr = aStr.substring(0, aStr.length() > 200 ? 200 : aStr.length())
                .replaceAll("	", " ")
                .replaceAll("\\x0c", "")
                .replaceAll("\\x0b", "")
                .replaceAll("\\x1e", "")
                .replaceAll("\\x1f", "");
        TransApi api = new TransApi(TLanguage.APP_ID, TLanguage.SECURITY_KEY);
        String mResult = api.getTransResult(mStr, "auto", "en");
        Map<String, Object> mMap = null;

        try {
            ObjectMapper FJson = new ObjectMapper();
            mMap = FJson.readValue(mResult,
                    new TypeReference<Map<String, Object>>() {
                    });
        } catch (Exception e) {
            e.printStackTrace();
            return new TWebResult(TApiState.API103, e.getMessage(), false);
        }

        if (mMap != null) {
            String mError = (String) mMap.get("error_code");
            if (StringUtils.isNotBlank(mError)) {
                return new TWebResult(TApiState.API115, (String) mMap.get("error_msg"), false);
            }
            TWebResult mRest = new TWebResult();
            mRest.setResult(true);
            Map<String, String> mMapRest = new HashMap<String, String>();
            List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
            mList.add(mMapRest);
            mRest.setData(mList);
            String baidulangId = (String) mMap.get("from");
            if (StringUtils.isBlank(baidulangId)) {
                mMapRest.put("langId", "0");
                mMapRest.put("langName", "语种获取失败");
            } else {
                mMapRest.put("langId",
                        LanguageList.getTransnLanguageId(TLanguageType.Baidu, baidulangId));
                mMapRest.put("langName", LanguageList.getLanguage(TLanguageType.Baidu, baidulangId));
            }
            return mRest;
        } else {
            return new TWebResult(TApiState.API115, false);
        }

    }


}
